home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / string < prev    next >
Encoding:
Text File  |  2000-06-08  |  74.4 KB  |  2,414 lines

  1. /*
  2.  * Copyright (c) 1997-1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Silicon Graphics makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  */ 
  13.  
  14. #ifndef __SGI_STL_STRING
  15. #define __SGI_STL_STRING
  16.  
  17. #include <stl_config.h>
  18. #include <stl_string_fwd.h>
  19. #include <ctype.h>        
  20. #include <functional>
  21. #include <stl_ctraits_fns.h>
  22. #include <stdexcept>      
  23. #include <stl_iterator_base.h>
  24. #include <memory>
  25. #include <algorithm>
  26.  
  27. #ifdef __STL_USE_NEW_IOSTREAMS
  28. #include <iosfwd>
  29. #else /* __STL_USE_NEW_IOSTREAMS */
  30. #include <char_traits.h>
  31. #endif /* __STL_USE_NEW_IOSTREAMS */
  32.  
  33. // Standard C++ string class.  This class has performance
  34. // characteristics very much like vector<>, meaning, for example, that
  35. // it does not perform reference-count or copy-on-write, and that
  36. // concatenation of two strings is an O(N) operation. 
  37.  
  38. // There are three reasons why basic_string is not identical to
  39. // vector.  First, basic_string always stores a null character at the
  40. // end; this makes it possible for c_str to be a fast operation.
  41. // Second, the C++ standard requires basic_string to copy elements
  42. // using char_traits<>::assign, char_traits<>::copy, and
  43. // char_traits<>::move.  This means that all of vector<>'s low-level
  44. // operations must be rewritten.  Third, basic_string<> has a lot of
  45. // extra functions in its interface that are convenient but, strictly
  46. // speaking, redundant.
  47.  
  48. // Additionally, the C++ standard imposes a major restriction: according
  49. // to the standard, the character type _CharT must be a POD type.  This
  50. // implementation weakens that restriction, and allows _CharT to be a
  51. // a user-defined non-POD type.  However, _CharT must still have a
  52. // default constructor.
  53.  
  54. __STL_BEGIN_NAMESPACE
  55.  
  56. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  57. #pragma set woff 1174
  58. #pragma set woff 1375
  59. #endif
  60.  
  61. // A helper class to use a char_traits as a function object.
  62.  
  63. template <class _Traits>
  64. struct _Not_within_traits
  65.   : public unary_function<typename _Traits::char_type, bool>
  66. {
  67.   typedef const typename _Traits::char_type* _Pointer;
  68.   const _Pointer _M_first;
  69.   const _Pointer _M_last;
  70.  
  71.   _Not_within_traits(_Pointer __f, _Pointer __l) 
  72.     : _M_first(__f), _M_last(__l) {}
  73.  
  74.   bool operator()(const typename _Traits::char_type& __x) const {
  75.     return find_if(_M_first, _M_last, 
  76.                    bind1st(_Eq_traits<_Traits>(), __x)) == _M_last;
  77.   }
  78. };
  79.  
  80. // ------------------------------------------------------------
  81. // Class _String_base.  
  82.  
  83. // _String_base is a helper class that makes it it easier to write an
  84. // exception-safe version of basic_string.  The constructor allocates,
  85. // but does not initialize, a block of memory.  The destructor
  86. // deallocates, but does not destroy elements within, a block of
  87. // memory.  The destructor assumes that _M_start either is null, or else
  88. // points to a block of memory that was allocated using _String_base's 
  89. // allocator and whose size is _M_end_of_storage - _M_start.
  90.  
  91. // Additionally, _String_base encapsulates the difference between
  92. // old SGI-style allocators and standard-conforming allocators.
  93.  
  94. #ifdef __STL_USE_STD_ALLOCATORS
  95.  
  96. // General base class.
  97. template <class _Tp, class _Alloc, bool _S_instanceless>
  98. class _String_alloc_base {
  99. public:
  100.   typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
  101.   allocator_type get_allocator() const { return _M_data_allocator; }
  102.  
  103.   _String_alloc_base(const allocator_type& __a)
  104.     : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
  105.     {}
  106.  
  107. protected:
  108.   _Tp* _M_allocate(size_t __n)
  109.     { return _M_data_allocator.allocate(__n); }
  110.   void _M_deallocate(_Tp* __p, size_t __n) {
  111.     if (__p)
  112.       _M_data_allocator.deallocate(__p, __n); 
  113.   }
  114.  
  115. protected:
  116.   allocator_type _M_data_allocator;
  117.  
  118.   _Tp* _M_start;
  119.   _Tp* _M_finish;
  120.   _Tp* _M_end_of_storage;
  121. };
  122.  
  123. // Specialization for instanceless allocators.
  124. template <class _Tp, class _Alloc>
  125. class _String_alloc_base<_Tp,_Alloc,true> {
  126. public:
  127.   typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
  128.   allocator_type get_allocator() const { return allocator_type(); }
  129.  
  130.   _String_alloc_base(const allocator_type&)
  131.     : _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
  132.  
  133. protected:
  134.   typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Alloc_type;
  135.   _Tp* _M_allocate(size_t __n)
  136.     { return _Alloc_type::allocate(__n); }
  137.   void _M_deallocate(_Tp* __p, size_t __n)
  138.     { _Alloc_type::deallocate(__p, __n); }
  139.  
  140. protected:
  141.   _Tp* _M_start;
  142.   _Tp* _M_finish;
  143.   _Tp* _M_end_of_storage;
  144. };
  145.  
  146. template <class _Tp, class _Alloc>
  147. class _String_base 
  148.   : public _String_alloc_base<_Tp, _Alloc,
  149.                               _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
  150. {
  151. protected:
  152.   typedef _String_alloc_base<_Tp, _Alloc,
  153.                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
  154.           _Base;
  155.   typedef typename _Base::allocator_type allocator_type;
  156.  
  157.   void _M_allocate_block(size_t __n) { 
  158.     if (__n <= max_size()) {
  159.       _M_start  = _M_allocate(__n);
  160.       _M_finish = _M_start;
  161.       _M_end_of_storage = _M_start + __n;
  162.     }
  163.     else
  164.       _M_throw_length_error();
  165.   }
  166.  
  167.   void _M_deallocate_block() 
  168.     { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
  169.   
  170.   size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
  171.  
  172.   _String_base(const allocator_type& __a) : _Base(__a) { }
  173.   
  174.   _String_base(const allocator_type& __a, size_t __n) : _Base(__a)
  175.     { _M_allocate_block(__n); }
  176.  
  177.   ~_String_base() { _M_deallocate_block(); }
  178.  
  179.   void _M_throw_length_error() const;
  180.   void _M_throw_out_of_range() const;
  181. };
  182.  
  183. #else /* __STL_USE_STD_ALLOCATORS */
  184.  
  185. template <class _Tp, class _Alloc> class _String_base {
  186. public:
  187.   typedef _Alloc allocator_type;
  188.   allocator_type get_allocator() const { return allocator_type(); }
  189.  
  190. protected:
  191.   typedef simple_alloc<_Tp, _Alloc> _Alloc_type;
  192.  
  193.   _Tp* _M_start;
  194.   _Tp* _M_finish;
  195.   _Tp* _M_end_of_storage;
  196.                                 // Precondition: 0 < __n <= max_size().
  197.  
  198.   _Tp* _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); }
  199.   void _M_deallocate(_Tp* __p, size_t __n) {
  200.     if (__p)
  201.       _Alloc_type::deallocate(__p, __n); 
  202.   }
  203.  
  204.   void _M_allocate_block(size_t __n) { 
  205.     if (__n <= max_size()) {
  206.       _M_start  = _M_allocate(__n);
  207.       _M_finish = _M_start;
  208.       _M_end_of_storage = _M_start + __n;
  209.     }
  210.     else
  211.       _M_throw_length_error();
  212.   }
  213.  
  214.   void _M_deallocate_block() 
  215.     { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
  216.   
  217.   size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
  218.  
  219.   _String_base(const allocator_type&)
  220.     : _M_start(0), _M_finish(0), _M_end_of_storage(0) { }
  221.   
  222.   _String_base(const allocator_type&, size_t __n)
  223.     : _M_start(0), _M_finish(0), _M_end_of_storage(0)
  224.     { _M_allocate_block(__n); }
  225.  
  226.   ~_String_base() { _M_deallocate_block(); }
  227.  
  228.   void _M_throw_length_error() const;
  229.   void _M_throw_out_of_range() const;
  230. };
  231.  
  232. #endif /* __STL_USE_STD_ALLOCATORS */
  233.  
  234. // Helper functions for exception handling.
  235. template <class _Tp, class _Alloc> 
  236. void _String_base<_Tp,_Alloc>::_M_throw_length_error() const {
  237.   __STL_THROW(length_error("basic_string"));
  238. }
  239.  
  240. template <class _Tp, class _Alloc> 
  241. void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const {
  242.   __STL_THROW(out_of_range("basic_string"));
  243. }
  244.  
  245.  
  246. // ------------------------------------------------------------
  247. // Class basic_string.  
  248.  
  249. // Class invariants:
  250. // (1) [start, finish) is a valid range.
  251. // (2) Each iterator in [start, finish) points to a valid object
  252. //     of type value_type.
  253. // (3) *finish is a valid object of type value_type; in particular,
  254. //     it is value_type().
  255. // (4) [finish + 1, end_of_storage) is a valid range.
  256. // (5) Each iterator in [finish + 1, end_of_storage) points to 
  257. //     unininitialized memory.
  258.  
  259. // Note one important consequence: a string of length n must manage
  260. // a block of memory whose size is at least n + 1.  
  261.  
  262.  
  263. template <class _CharT, class _Traits, class _Alloc> 
  264. class basic_string : private _String_base<_CharT,_Alloc> {
  265. public:
  266.   typedef _CharT value_type;
  267.   typedef _Traits traits_type;
  268.  
  269.   typedef value_type* pointer;
  270.   typedef const value_type* const_pointer;
  271.   typedef value_type& reference;
  272.   typedef const value_type& const_reference;
  273.   typedef size_t size_type;
  274.   typedef ptrdiff_t difference_type;
  275.  
  276.   typedef const value_type*                const_iterator;
  277.   typedef value_type*                      iterator;
  278.  
  279. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  280.   typedef reverse_iterator<const_iterator> const_reverse_iterator;
  281.   typedef reverse_iterator<iterator>       reverse_iterator;
  282. #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  283.   typedef reverse_iterator<const_iterator, value_type, const_reference, 
  284.                            difference_type>  
  285.           const_reverse_iterator;
  286.   typedef reverse_iterator<iterator, value_type, reference, difference_type>
  287.           reverse_iterator; 
  288. #endif /* __STL_PARTIAL_SPECIALIZATION */
  289.  
  290.   static const size_type npos;
  291.  
  292.   typedef _String_base<_CharT,_Alloc> _Base;
  293.  
  294. public:                         // Constructor, destructor, assignment.
  295.   typedef typename _Base::allocator_type allocator_type;
  296.   allocator_type get_allocator() const { return _Base::get_allocator(); }
  297.  
  298.   explicit basic_string(const allocator_type& __a = allocator_type())
  299.     : _Base(__a, 8) { _M_terminate_string(); }
  300.  
  301.   struct _Reserve_t {};
  302.   basic_string(_Reserve_t, size_t __n,
  303.                const allocator_type& __a = allocator_type())
  304.     : _Base(__a, __n + 1) { _M_terminate_string(); }
  305.  
  306.   basic_string(const basic_string& __s) : _Base(__s.get_allocator()) 
  307.     { _M_range_initialize(__s.begin(), __s.end()); }
  308.  
  309.   basic_string(const basic_string& __s, size_type __pos, size_type __n = npos,
  310.                const allocator_type& __a = allocator_type()) 
  311.     : _Base(__a) {
  312.     if (__pos > __s.size())
  313.       _M_throw_out_of_range();
  314.     else
  315.       _M_range_initialize(__s.begin() + __pos,
  316.                           __s.begin() + __pos + min(__n, __s.size() - __pos));
  317.   }
  318.  
  319.   basic_string(const _CharT* __s, size_type __n,
  320.                const allocator_type& __a = allocator_type()) 
  321.     : _Base(__a) 
  322.     { _M_range_initialize(__s, __s + __n); }
  323.  
  324.   basic_string(const _CharT* __s,
  325.                const allocator_type& __a = allocator_type())
  326.     : _Base(__a) 
  327.     { _M_range_initialize(__s, __s + _Traits::length(__s)); }
  328.  
  329.   basic_string(size_type __n, _CharT __c,
  330.                const allocator_type& __a = allocator_type())
  331.     : _Base(__a, __n + 1)
  332.   {
  333.     _M_finish = uninitialized_fill_n(_M_start, __n, __c);
  334.     _M_terminate_string();
  335.   }
  336.  
  337.   // Check to see if _InputIterator is an integer type.  If so, then
  338.   // it can't be an iterator.
  339. #ifdef __STL_MEMBER_TEMPLATES
  340.   template <class _InputIterator>
  341.   basic_string(_InputIterator __f, _InputIterator __l,
  342.                const allocator_type& __a = allocator_type())
  343.     : _Base(__a)
  344.   {
  345.     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  346.     _M_initialize_dispatch(__f, __l, _Integral());
  347.   }
  348. #else /* __STL_MEMBER_TEMPLATES */
  349.   basic_string(const _CharT* __f, const _CharT* __l,
  350.                const allocator_type& __a = allocator_type())
  351.     : _Base(__a)
  352.   {
  353.     _M_range_initialize(__f, __l);
  354.   }
  355. #endif
  356.  
  357.   ~basic_string() { destroy(_M_start, _M_finish + 1); }
  358.     
  359.   basic_string& operator=(const basic_string& __s) {
  360.     if (&__s != this) 
  361.       assign(__s.begin(), __s.end());
  362.     return *this;
  363.   }
  364.  
  365.   basic_string& operator=(const _CharT* __s) 
  366.     { return assign(__s, __s + _Traits::length(__s)); }
  367.  
  368.   basic_string& operator=(_CharT __c)
  369.     { return assign(static_cast<size_type>(1), __c); }
  370.  
  371. protected:                      // Protected members inherited from base.
  372. #ifdef __STL_HAS_NAMESPACES
  373.   using _Base::_M_allocate;
  374.   using _Base::_M_deallocate;
  375.   using _Base::_M_allocate_block;
  376.   using _Base::_M_deallocate_block;
  377.   using _Base::_M_throw_length_error;
  378.   using _Base::_M_throw_out_of_range;
  379.  
  380.   using _Base::_M_start;
  381.   using _Base::_M_finish;
  382.   using _Base::_M_end_of_storage;
  383. #endif /* __STL_HAS_NAMESPACES */
  384.  
  385. private:                        // Helper functions used by constructors
  386.                                 // and elsewhere.
  387.   void _M_construct_null(_CharT* __p) {
  388.     construct(__p);
  389. #   ifdef __STL_DEFAULT_CONSTRUCTOR_BUG
  390.     __STL_TRY {
  391.       *__p = (_CharT) 0;
  392.     }
  393.     __STL_UNWIND(destroy(__p));
  394. #   endif
  395.   }
  396.  
  397.   static _CharT _M_null() {
  398. #   ifndef __STL_DEFAULT_CONSTRUCTOR_BUG
  399.     return _CharT();
  400. #   else
  401.     return (_CharT) 0;
  402. #   endif
  403.   }
  404.  
  405. private:                        
  406.   // Helper functions used by constructors.  It is a severe error for
  407.   // any of them to be called anywhere except from within constructors.
  408.  
  409.   void _M_terminate_string() {
  410.     __STL_TRY {
  411.       _M_construct_null(_M_finish);
  412.     }
  413.     __STL_UNWIND(destroy(_M_start, _M_finish));
  414.   }
  415.  
  416. #ifdef __STL_MEMBER_TEMPLATES
  417.     
  418.   template <class _InputIter>
  419.   void _M_range_initialize(_InputIter __f, _InputIter __l,
  420.                            input_iterator_tag) {
  421.     _M_allocate_block(8);
  422.     _M_construct_null(_M_finish);
  423.     __STL_TRY {
  424.       append(__f, __l);
  425.     }
  426.     __STL_UNWIND(destroy(_M_start, _M_finish + 1));
  427.   }
  428.  
  429.   template <class _ForwardIter>
  430.   void _M_range_initialize(_ForwardIter __f, _ForwardIter __l, 
  431.                            forward_iterator_tag) {
  432.     difference_type __n = 0;
  433.     distance(__f, __l, __n);
  434.     _M_allocate_block(__n + 1);
  435.     _M_finish = uninitialized_copy(__f, __l, _M_start);
  436.     _M_terminate_string();
  437.   }
  438.  
  439.   template <class _InputIter>
  440.   void _M_range_initialize(_InputIter __f, _InputIter __l) {
  441.     typedef typename iterator_traits<_InputIter>::iterator_category _Category;
  442.     _M_range_initialize(__f, __l, _Category());
  443.   }
  444.  
  445.   template <class _Integer>
  446.   void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
  447.     _M_allocate_block(__n + 1);
  448.     _M_finish = uninitialized_fill_n(_M_start, __n, __x);
  449.     _M_terminate_string();
  450.   }
  451.  
  452.   template <class _InputIter>
  453.   void _M_initialize_dispatch(_InputIter __f, _InputIter __l, __false_type) {
  454.      _M_range_initialize(__f, __l);
  455.   }
  456.     
  457. #else /* __STL_MEMBER_TEMPLATES */
  458.  
  459.   void _M_range_initialize(const _CharT* __f, const _CharT* __l) {
  460.     ptrdiff_t __n = __l - __f;
  461.     _M_allocate_block(__n + 1);
  462.     _M_finish = uninitialized_copy(__f, __l, _M_start);
  463.     _M_terminate_string();
  464.   }
  465.  
  466. #endif /* __STL_MEMBER_TEMPLATES */
  467.  
  468. public:                         // Iterators.
  469.   iterator begin()             { return _M_start; }
  470.   iterator end()               { return _M_finish; }
  471.   const_iterator begin() const { return _M_start; }
  472.   const_iterator end()   const { return _M_finish; }  
  473.  
  474.   reverse_iterator rbegin()             
  475.     { return reverse_iterator(_M_finish); }
  476.   reverse_iterator rend()               
  477.     { return reverse_iterator(_M_start); }
  478.   const_reverse_iterator rbegin() const 
  479.     { return const_reverse_iterator(_M_finish); }
  480.   const_reverse_iterator rend()   const 
  481.     { return const_reverse_iterator(_M_start); }
  482.  
  483. public:                         // Size, capacity, etc.
  484.   size_type size() const { return _M_finish - _M_start; }
  485.   size_type length() const { return size(); }
  486.  
  487.   size_t max_size() const { return _Base::max_size(); }
  488.  
  489.  
  490.   void resize(size_type __n, _CharT __c) {
  491.     if (__n <= size())
  492.       erase(begin() + __n, end());
  493.     else
  494.       append(__n - size(), __c);
  495.   }
  496.  
  497.   void resize(size_type __n) { resize(__n, _M_null()); }
  498.  
  499.   void reserve(size_type = 0);
  500.  
  501.   size_type capacity() const { return (_M_end_of_storage - _M_start) - 1; }
  502.  
  503.   void clear() {
  504.     if (!empty()) {
  505.       _Traits::assign(*_M_start, _M_null());
  506.       destroy(_M_start+1, _M_finish+1);
  507.       _M_finish = _M_start;
  508.     }
  509.   } 
  510.  
  511.   bool empty() const { return _M_start == _M_finish; }    
  512.  
  513. public:                         // Element access.
  514.  
  515.   const_reference operator[](size_type __n) const
  516.     { return *(_M_start + __n); }
  517.   reference operator[](size_type __n)
  518.     { return *(_M_start + __n); }
  519.  
  520.   const_reference at(size_type __n) const {
  521.     if (__n >= size())
  522.       _M_throw_out_of_range();
  523.     return *(_M_start + __n);
  524.   }
  525.  
  526.   reference at(size_type __n) {
  527.     if (__n >= size())
  528.       _M_throw_out_of_range();
  529.     return *(_M_start + __n);
  530.   }
  531.  
  532. public:                         // Append, operator+=, push_back.
  533.  
  534.   basic_string& operator+=(const basic_string& __s) { return append(__s); }
  535.   basic_string& operator+=(const _CharT* __s) { return append(__s); }
  536.   basic_string& operator+=(_CharT __c) { push_back(__c); return *this; }
  537.  
  538.   basic_string& append(const basic_string& __s) 
  539.     { return append(__s.begin(), __s.end()); }
  540.  
  541.   basic_string& append(const basic_string& __s,
  542.                        size_type __pos, size_type __n)
  543.   {
  544.     if (__pos > __s.size())
  545.       _M_throw_out_of_range();
  546.     return append(__s.begin() + __pos,
  547.                   __s.begin() + __pos + min(__n, __s.size() - __pos));
  548.   }
  549.  
  550.   basic_string& append(const _CharT* __s, size_type __n) 
  551.     { return append(__s, __s+__n); }
  552.  
  553.   basic_string& append(const _CharT* __s) 
  554.     { return append(__s, __s + _Traits::length(__s)); }
  555.  
  556.   basic_string& append(size_type __n, _CharT __c);
  557.  
  558. #ifdef __STL_MEMBER_TEMPLATES
  559.  
  560.   // Check to see if _InputIterator is an integer type.  If so, then
  561.   // it can't be an iterator.
  562.   template <class _InputIter>
  563.   basic_string& append(_InputIter __first, _InputIter __last) {
  564.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  565.     return _M_append_dispatch(__first, __last, _Integral());
  566.   }
  567.  
  568. #else /* __STL_MEMBER_TEMPLATES */
  569.  
  570.   basic_string& append(const _CharT* __first, const _CharT* __last);
  571.  
  572. #endif /* __STL_MEMBER_TEMPLATES */
  573.  
  574.   void push_back(_CharT __c) {
  575.     if (_M_finish + 1 == _M_end_of_storage)
  576.       reserve(size() + max(size(), static_cast<size_type>(1)));
  577.     _M_construct_null(_M_finish + 1);
  578.     _Traits::assign(*_M_finish, __c);
  579.     ++_M_finish;
  580.   }
  581.  
  582.   void pop_back() {
  583.     _Traits::assign(*(_M_finish - 1), _M_null());
  584.     destroy(_M_finish);
  585.     --_M_finish;
  586.   }
  587.  
  588. private:                        // Helper functions for append.
  589.  
  590. #ifdef __STL_MEMBER_TEMPLATES
  591.  
  592.   template <class _InputIter>
  593.   basic_string& append(_InputIter __f, _InputIter __l, input_iterator_tag);
  594.  
  595.   template <class _ForwardIter>
  596.   basic_string& append(_ForwardIter __f, _ForwardIter __l, 
  597.                        forward_iterator_tag);
  598.  
  599.   template <class _Integer>
  600.   basic_string& _M_append_dispatch(_Integer __n, _Integer __x, __true_type) {
  601.     return append((size_type) __n, (_CharT) __x);
  602.   }
  603.  
  604.   template <class _InputIter>
  605.   basic_string& _M_append_dispatch(_InputIter __f, _InputIter __l,
  606.                                    __false_type) {
  607.     typedef typename iterator_traits<_InputIter>::iterator_category _Category;
  608.     return append(__f, __l, _Category());
  609.   }
  610.  
  611. #endif /* __STL_MEMBER_TEMPLATES */
  612.  
  613. public:                         // Assign
  614.   
  615.   basic_string& assign(const basic_string& __s) 
  616.     { return assign(__s.begin(), __s.end()); }
  617.  
  618.   basic_string& assign(const basic_string& __s, 
  619.                        size_type __pos, size_type __n) {
  620.     if (__pos > __s.size())
  621.       _M_throw_out_of_range();
  622.     return assign(__s.begin() + __pos, 
  623.                   __s.begin() + __pos + min(__n, __s.size() - __pos));
  624.   }
  625.  
  626.   basic_string& assign(const _CharT* __s, size_type __n)
  627.     { return assign(__s, __s + __n); }
  628.  
  629.   basic_string& assign(const _CharT* __s)
  630.     { return assign(__s, __s + _Traits::length(__s)); }
  631.  
  632.   basic_string& assign(size_type __n, _CharT __c);
  633.  
  634. #ifdef __STL_MEMBER_TEMPLATES
  635.  
  636.   // Check to see if _InputIterator is an integer type.  If so, then
  637.   // it can't be an iterator.
  638.   template <class _InputIter>
  639.   basic_string& assign(_InputIter __first, _InputIter __last) {
  640.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  641.     return _M_assign_dispatch(__first, __last, _Integral());
  642.   }
  643.  
  644. #endif  /* __STL_MEMBER_TEMPLATES */
  645.  
  646.   basic_string& assign(const _CharT* __f, const _CharT* __l);
  647.  
  648. private:                        // Helper functions for assign.
  649.  
  650. #ifdef __STL_MEMBER_TEMPLATES
  651.  
  652.   template <class _Integer>
  653.   basic_string& _M_assign_dispatch(_Integer __n, _Integer __x, __true_type) {
  654.     return assign((size_type) __n, (_CharT) __x);
  655.   }
  656.  
  657.   template <class _InputIter>
  658.   basic_string& _M_assign_dispatch(_InputIter __f, _InputIter __l,
  659.                                    __false_type);
  660.  
  661. #endif  /* __STL_MEMBER_TEMPLATES */
  662.  
  663. public:                         // Insert
  664.  
  665.   basic_string& insert(size_type __pos, const basic_string& __s) {
  666.     if (__pos > size())
  667.       _M_throw_out_of_range();
  668.     if (size() > max_size() - __s.size())
  669.       _M_throw_length_error();
  670.     insert(_M_start + __pos, __s.begin(), __s.end());
  671.     return *this;
  672.   }
  673.  
  674.   basic_string& insert(size_type __pos, const basic_string& __s,
  675.                        size_type __beg, size_type __n) {
  676.     if (__pos > size() || __beg > __s.size())
  677.       _M_throw_out_of_range();
  678.     size_type __len = min(__n, __s.size() - __beg);
  679.     if (size() > max_size() - __len)
  680.       _M_throw_length_error();
  681.     insert(_M_start + __pos,
  682.            __s.begin() + __beg, __s.begin() + __beg + __len);
  683.     return *this;
  684.   }
  685.  
  686.   basic_string& insert(size_type __pos, const _CharT* __s, size_type __n) {
  687.     if (__pos > size())
  688.       _M_throw_out_of_range();
  689.     if (size() > max_size() - __n)
  690.       _M_throw_length_error();
  691.     insert(_M_start + __pos, __s, __s + __n);
  692.     return *this;
  693.   }
  694.  
  695.   basic_string& insert(size_type __pos, const _CharT* __s) {
  696.     if (__pos > size())
  697.       _M_throw_out_of_range();
  698.     size_type __len = _Traits::length(__s);
  699.     if (size() > max_size() - __len)
  700.       _M_throw_length_error();
  701.     insert(_M_start + __pos, __s, __s + __len);
  702.     return *this;
  703.   }
  704.     
  705.   basic_string& insert(size_type __pos, size_type __n, _CharT __c) {
  706.     if (__pos > size())
  707.       _M_throw_out_of_range();
  708.     if (size() > max_size() - __n)
  709.       _M_throw_length_error();
  710.     insert(_M_start + __pos, __n, __c);
  711.     return *this;
  712.   }
  713.  
  714.   iterator insert(iterator __p, _CharT __c) {
  715.     if (__p == _M_finish) {
  716.       push_back(__c);
  717.       return _M_finish - 1;
  718.     }
  719.     else
  720.       return _M_insert_aux(__p, __c);
  721.   }
  722.  
  723.   void insert(iterator __p, size_t __n, _CharT __c);
  724.  
  725. #ifdef __STL_MEMBER_TEMPLATES
  726.  
  727.   // Check to see if _InputIterator is an integer type.  If so, then
  728.   // it can't be an iterator.
  729.   template <class _InputIter>
  730.   void insert(iterator __p, _InputIter __first, _InputIter __last) {
  731.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  732.     _M_insert_dispatch(__p, __first, __last, _Integral());
  733.   }
  734.  
  735. #else /* __STL_MEMBER_TEMPLATES */
  736.  
  737.   void insert(iterator __p, const _CharT* __first, const _CharT* __last);
  738.  
  739. #endif /* __STL_MEMBER_TEMPLATES */
  740.  
  741. private:                        // Helper functions for insert.
  742.  
  743. #ifdef __STL_MEMBER_TEMPLATES
  744.  
  745.   template <class _InputIter>
  746.   void insert(iterator __p, _InputIter, _InputIter, input_iterator_tag);
  747.  
  748.   template <class _ForwardIter>
  749.   void insert(iterator __p, _ForwardIter, _ForwardIter, forward_iterator_tag);
  750.  
  751.  
  752.   template <class _Integer>
  753.   void _M_insert_dispatch(iterator __p, _Integer __n, _Integer __x,
  754.                           __true_type) {
  755.     insert(__p, (size_type) __n, (_CharT) __x);
  756.   }
  757.  
  758.   template <class _InputIter>
  759.   void _M_insert_dispatch(iterator __p, _InputIter __first, _InputIter __last,
  760.                           __false_type) {
  761.     typedef typename iterator_traits<_InputIter>::iterator_category _Category;
  762.     insert(__p, __first, __last, _Category());
  763.   }
  764.  
  765.   template <class _InputIterator>
  766.   void 
  767.   _M_copy(_InputIterator __first, _InputIterator __last, iterator __result) {
  768.     for ( ; __first != __last; ++__first, ++__result)
  769.       _Traits::assign(*__result, *__first);
  770.   }
  771.  
  772. #endif /* __STL_MEMBER_TEMPLATES */
  773.  
  774.   iterator _M_insert_aux(iterator, _CharT);
  775.  
  776.   void 
  777.   _M_copy(const _CharT* __first, const _CharT* __last, _CharT* __result) {
  778.     _Traits::copy(__result, __first, __last - __first);
  779.   }
  780.  
  781. public:                         // Erase.
  782.  
  783.   basic_string& erase(size_type __pos = 0, size_type __n = npos) {
  784.     if (__pos > size())
  785.       _M_throw_out_of_range();
  786.     erase(_M_start + __pos, _M_start + __pos + min(__n, size() - __pos));
  787.     return *this;
  788.   }  
  789.  
  790.   iterator erase(iterator __position) {
  791.                                 // The move includes the terminating null.
  792.     _Traits::move(__position, __position + 1, _M_finish - __position);
  793.     destroy(_M_finish);
  794.     --_M_finish;
  795.     return __position;
  796.   }
  797.  
  798.   iterator erase(iterator __first, iterator __last) {
  799.     if (__first != __last) {
  800.                                 // The move includes the terminating null.
  801.       _Traits::move(__first, __last, (_M_finish - __last) + 1);
  802.       const iterator __new_finish = _M_finish - (__last - __first);
  803.       destroy(__new_finish + 1, _M_finish + 1);
  804.       _M_finish = __new_finish;
  805.     }
  806.     return __first;
  807.   }
  808.  
  809. public:                         // Replace.  (Conceptually equivalent
  810.                                 // to erase followed by insert.)
  811.   basic_string& replace(size_type __pos, size_type __n, 
  812.                         const basic_string& __s) {
  813.     if (__pos > size())
  814.       _M_throw_out_of_range();
  815.     const size_type __len = min(__n, size() - __pos);
  816.     if (size() - __len >= max_size() - __s.size())
  817.       _M_throw_length_error();
  818.     return replace(_M_start + __pos, _M_start + __pos + __len, 
  819.                    __s.begin(), __s.end());
  820.   }
  821.  
  822.   basic_string& replace(size_type __pos1, size_type __n1,
  823.                         const basic_string& __s,
  824.                         size_type __pos2, size_type __n2) {
  825.     if (__pos1 > size() || __pos2 > __s.size())
  826.       _M_throw_out_of_range();
  827.     const size_type __len1 = min(__n1, size() - __pos1);
  828.     const size_type __len2 = min(__n2, __s.size() - __pos2);
  829.     if (size() - __len1 >= max_size() - __len2)
  830.       _M_throw_length_error();
  831.     return replace(_M_start + __pos1, _M_start + __pos1 + __len1,
  832.                    __s._M_start + __pos2, __s._M_start + __pos2 + __len2);
  833.   }
  834.  
  835.   basic_string& replace(size_type __pos, size_type __n1,
  836.                         const _CharT* __s, size_type __n2) {
  837.     if (__pos > size())
  838.       _M_throw_out_of_range();
  839.     const size_type __len = min(__n1, size() - __pos);
  840.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  841.       _M_throw_length_error();
  842.     return replace(_M_start + __pos, _M_start + __pos + __len,
  843.                    __s, __s + __n2);
  844.   }
  845.  
  846.   basic_string& replace(size_type __pos, size_type __n1,
  847.                         const _CharT* __s) {
  848.     if (__pos > size())
  849.       _M_throw_out_of_range();
  850.     const size_type __len = min(__n1, size() - __pos);
  851.     const size_type __n2 = _Traits::length(__s);
  852.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  853.       _M_throw_length_error();
  854.     return replace(_M_start + __pos, _M_start + __pos + __len,
  855.                    __s, __s + _Traits::length(__s));
  856.   }
  857.  
  858.   basic_string& replace(size_type __pos, size_type __n1,
  859.                         size_type __n2, _CharT __c) {
  860.     if (__pos > size())
  861.       _M_throw_out_of_range();
  862.     const size_type __len = min(__n1, size() - __pos);
  863.     if (__n2 > max_size() || size() - __len >= max_size() - __n2)
  864.       _M_throw_length_error();
  865.     return replace(_M_start + __pos, _M_start + __pos + __len, __n2, __c);
  866.   }
  867.  
  868.   basic_string& replace(iterator __first, iterator __last, 
  869.                         const basic_string& __s) 
  870.     { return replace(__first, __last, __s.begin(), __s.end()); }
  871.  
  872.   basic_string& replace(iterator __first, iterator __last,
  873.                         const _CharT* __s, size_type __n) 
  874.     { return replace(__first, __last, __s, __s + __n); }
  875.  
  876.   basic_string& replace(iterator __first, iterator __last,
  877.                         const _CharT* __s) {
  878.     return replace(__first, __last, __s, __s + _Traits::length(__s));
  879.   }
  880.  
  881.   basic_string& replace(iterator __first, iterator __last, 
  882.                         size_type __n, _CharT __c);
  883.  
  884.   // Check to see if _InputIterator is an integer type.  If so, then
  885.   // it can't be an iterator.
  886. #ifdef __STL_MEMBER_TEMPLATES
  887.   template <class _InputIter>
  888.   basic_string& replace(iterator __first, iterator __last,
  889.                         _InputIter __f, _InputIter __l) {
  890.     typedef typename _Is_integer<_InputIter>::_Integral _Integral;
  891.     return _M_replace_dispatch(__first, __last, __f, __l,  _Integral());
  892.   }
  893. #else /* __STL_MEMBER_TEMPLATES */
  894.   basic_string& replace(iterator __first, iterator __last,
  895.                         const _CharT* __f, const _CharT* __l);
  896. #endif /* __STL_MEMBER_TEMPLATES */
  897.  
  898. private:                        // Helper functions for replace.
  899.  
  900. #ifdef __STL_MEMBER_TEMPLATES
  901.  
  902.   template <class _Integer>
  903.   basic_string& _M_replace_dispatch(iterator __first, iterator __last,
  904.                                     _Integer __n, _Integer __x,
  905.                                     __true_type) {
  906.     return replace(__first, __last, (size_type) __n, (_CharT) __x);
  907.   }
  908.  
  909.   template <class _InputIter>
  910.   basic_string& _M_replace_dispatch(iterator __first, iterator __last,
  911.                                     _InputIter __f, _InputIter __l,
  912.                                     __false_type) {
  913.     typedef typename iterator_traits<_InputIter>::iterator_category _Category;
  914.     return replace(__first, __last, __f, __l, _Category());
  915.   }
  916.  
  917.   template <class _InputIter>
  918.   basic_string& replace(iterator __first, iterator __last,
  919.                         _InputIter __f, _InputIter __l, input_iterator_tag);
  920.  
  921.   template <class _ForwardIter>
  922.   basic_string& replace(iterator __first, iterator __last,
  923.                         _ForwardIter __f, _ForwardIter __l, 
  924.                         forward_iterator_tag);
  925.  
  926. #endif /* __STL_MEMBER_TEMPLATES */
  927.  
  928. public:                         // Other modifier member functions.
  929.  
  930.   size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
  931.     if (__pos > size())
  932.       _M_throw_out_of_range();
  933.     const size_type __len = min(__n, size() - __pos);
  934.     _Traits::copy(__s, _M_start + __pos, __len);
  935.     return __len;
  936.   }
  937.  
  938.   void swap(basic_string& __s) {
  939.     __STD::swap(_M_start, __s._M_start);
  940.     __STD::swap(_M_finish, __s._M_finish);
  941.     __STD::swap(_M_end_of_storage, __s._M_end_of_storage);
  942.   }
  943.  
  944. public:                         // Conversion to C string.
  945.  
  946.   const _CharT* c_str() const { return _M_start; }
  947.   const _CharT* data()  const { return _M_start; }
  948.  
  949. public:                         // find.
  950.  
  951.   size_type find(const basic_string& __s, size_type __pos = 0) const 
  952.     { return find(__s.begin(), __pos, __s.size()); }
  953.  
  954.   size_type find(const _CharT* __s, size_type __pos = 0) const 
  955.     { return find(__s, __pos, _Traits::length(__s)); }
  956.  
  957.   size_type find(const _CharT* __s, size_type __pos, size_type __n) const;
  958.   size_type find(_CharT __c, size_type __pos = 0) const;
  959.  
  960. public:                         // rfind.
  961.  
  962.   size_type rfind(const basic_string& __s, size_type __pos = npos) const 
  963.     { return rfind(__s.begin(), __pos, __s.size()); }
  964.  
  965.   size_type rfind(const _CharT* __s, size_type __pos = npos) const 
  966.     { return rfind(__s, __pos, _Traits::length(__s)); }
  967.  
  968.   size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const;
  969.   size_type rfind(_CharT __c, size_type __pos = npos) const;
  970.  
  971. public:                         // find_first_of
  972.   
  973.   size_type find_first_of(const basic_string& __s, size_type __pos = 0) const 
  974.     { return find_first_of(__s.begin(), __pos, __s.size()); }
  975.  
  976.   size_type find_first_of(const _CharT* __s, size_type __pos = 0) const 
  977.     { return find_first_of(__s, __pos, _Traits::length(__s)); }
  978.  
  979.   size_type find_first_of(const _CharT* __s, size_type __pos, 
  980.                           size_type __n) const;
  981.  
  982.   size_type find_first_of(_CharT __c, size_type __pos = 0) const 
  983.     { return find(__c, __pos); }
  984.  
  985. public:                         // find_last_of
  986.  
  987.   size_type find_last_of(const basic_string& __s,
  988.                          size_type __pos = npos) const
  989.     { return find_last_of(__s.begin(), __pos, __s.size()); }
  990.  
  991.   size_type find_last_of(const _CharT* __s, size_type __pos = npos) const 
  992.     { return find_last_of(__s, __pos, _Traits::length(__s)); }
  993.  
  994.   size_type find_last_of(const _CharT* __s, size_type __pos, 
  995.                          size_type __n) const;
  996.  
  997.   size_type find_last_of(_CharT __c, size_type __pos = npos) const {
  998.     return rfind(__c, __pos);
  999.   }
  1000.  
  1001. public:                         // find_first_not_of
  1002.  
  1003.   size_type find_first_not_of(const basic_string& __s, 
  1004.                               size_type __pos = 0) const 
  1005.     { return find_first_not_of(__s.begin(), __pos, __s.size()); }
  1006.  
  1007.   size_type find_first_not_of(const _CharT* __s, size_type __pos = 0) const 
  1008.     { return find_first_not_of(__s, __pos, _Traits::length(__s)); }
  1009.  
  1010.   size_type find_first_not_of(const _CharT* __s, size_type __pos,
  1011.                               size_type __n) const;
  1012.  
  1013.   size_type find_first_not_of(_CharT __c, size_type __pos = 0) const;
  1014.  
  1015. public:                         // find_last_not_of
  1016.  
  1017.   size_type find_last_not_of(const basic_string& __s, 
  1018.                              size_type __pos = npos) const
  1019.     { return find_last_not_of(__s.begin(), __pos, __s.size()); }
  1020.  
  1021.   size_type find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  1022.     { return find_last_not_of(__s, __pos, _Traits::length(__s)); }
  1023.  
  1024.   size_type find_last_not_of(const _CharT* __s, size_type __pos,
  1025.                              size_type __n) const;
  1026.  
  1027.   size_type find_last_not_of(_CharT __c, size_type __pos = npos) const;
  1028.  
  1029. public:                         // Substring.
  1030.  
  1031.   basic_string substr(size_type __pos = 0, size_type __n = npos) const {
  1032.     if (__pos > size())
  1033.       _M_throw_out_of_range();
  1034.     return basic_string(_M_start + __pos, 
  1035.                         _M_start + __pos + min(__n, size() - __pos));
  1036.   }
  1037.  
  1038. public:                         // Compare
  1039.  
  1040.   int compare(const basic_string& __s) const 
  1041.     { return _M_compare(_M_start, _M_finish, __s._M_start, __s._M_finish); }
  1042.  
  1043.   int compare(size_type __pos1, size_type __n1,
  1044.               const basic_string& __s) const {
  1045.     if (__pos1 > size())
  1046.       _M_throw_out_of_range();
  1047.     return _M_compare(_M_start + __pos1, 
  1048.                       _M_start + __pos1 + min(__n1, size() - __pos1),
  1049.                       __s._M_start, __s._M_finish);
  1050.   }
  1051.     
  1052.   int compare(size_type __pos1, size_type __n1,
  1053.               const basic_string& __s,
  1054.               size_type __pos2, size_type __n2) const {
  1055.     if (__pos1 > size() || __pos2 > __s.size())
  1056.       _M_throw_out_of_range();
  1057.     return _M_compare(_M_start + __pos1, 
  1058.                       _M_start + __pos1 + min(__n1, size() - __pos1),
  1059.                       __s._M_start + __pos2, 
  1060.                       __s._M_start + __pos2 + min(__n2, size() - __pos2));
  1061.   }
  1062.  
  1063.   int compare(const _CharT* __s) const {
  1064.     return _M_compare(_M_start, _M_finish, __s, __s + _Traits::length(__s));
  1065.   }
  1066.  
  1067.   int compare(size_type __pos1, size_type __n1, const _CharT* __s) const {
  1068.     if (__pos1 > size())
  1069.       _M_throw_out_of_range();
  1070.     return _M_compare(_M_start + __pos1, 
  1071.                       _M_start + __pos1 + min(__n1, size() - __pos1),
  1072.                       __s, __s + _Traits::length(__s));
  1073.   }
  1074.  
  1075.   int compare(size_type __pos1, size_type __n1, const _CharT* __s,
  1076.               size_type __n2) const {
  1077.     if (__pos1 > size())
  1078.       _M_throw_out_of_range();
  1079.     return _M_compare(_M_start + __pos1, 
  1080.                       _M_start + __pos1 + min(__n1, size() - __pos1),
  1081.                       __s, __s + __n2);
  1082.   }
  1083.  
  1084. public:                        // Helper function for compare.
  1085.   static int _M_compare(const _CharT* __f1, const _CharT* __l1,
  1086.                         const _CharT* __f2, const _CharT* __l2) {
  1087.     const ptrdiff_t __n1 = __l1 - __f1;
  1088.     const ptrdiff_t __n2 = __l2 - __f2;
  1089.     const int cmp = _Traits::compare(__f1, __f2, min(__n1, __n2));
  1090.     return cmp != 0 ? cmp : (__n1 < __n2 ? -1 : (__n1 > __n2 ? 1 : 0));
  1091.   }
  1092. };
  1093.  
  1094.  
  1095.  
  1096. // ------------------------------------------------------------
  1097. // Non-inline declarations.
  1098.  
  1099. template <class _CharT, class _Traits, class _Alloc> 
  1100. const basic_string<_CharT,_Traits,_Alloc>::size_type 
  1101. basic_string<_CharT,_Traits,_Alloc>::npos 
  1102.   = (basic_string<_CharT,_Traits,_Alloc>::size_type) -1;
  1103.  
  1104. // Change the string's capacity so that it is large enough to hold
  1105. //  at least __res_arg elements, plus the terminating null.  Note that,
  1106. //  if __res_arg < capacity(), this member function may actually decrease
  1107. //  the string's capacity.
  1108. template <class _CharT, class _Traits, class _Alloc> 
  1109. void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
  1110.   if (__res_arg > max_size())
  1111.     _M_throw_length_error();
  1112.  
  1113.   size_type __n = max(__res_arg, size()) + 1;
  1114.   pointer __new_start = _M_allocate(__n);
  1115.   pointer __new_finish = __new_start;
  1116.  
  1117.   __STL_TRY {
  1118.     __new_finish = uninitialized_copy(_M_start, _M_finish, __new_start);
  1119.     _M_construct_null(__new_finish);
  1120.   }
  1121.   __STL_UNWIND((destroy(__new_start, __new_finish), 
  1122.                 _M_deallocate(__new_start, __n)));
  1123.  
  1124.   destroy(_M_start, _M_finish + 1);
  1125.   _M_deallocate_block();
  1126.   _M_start = __new_start;
  1127.   _M_finish = __new_finish;
  1128.   _M_end_of_storage = __new_start + __n;
  1129. }
  1130.  
  1131. template <class _CharT, class _Traits, class _Alloc> 
  1132. basic_string<_CharT,_Traits,_Alloc>& 
  1133. basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
  1134.   if (__n > max_size() || size() > max_size() - __n)
  1135.     _M_throw_length_error();
  1136.   if (size() + __n > capacity())
  1137.     reserve(size() + max(size(), __n));
  1138.   if (__n > 0) {
  1139.     uninitialized_fill_n(_M_finish + 1, __n - 1, __c);
  1140.     __STL_TRY {
  1141.       _M_construct_null(_M_finish + __n);
  1142.     }
  1143.     __STL_UNWIND(destroy(_M_finish + 1, _M_finish + __n));
  1144.     _Traits::assign(*_M_finish, __c);
  1145.     _M_finish += __n;
  1146.   }
  1147.   return *this;
  1148. }
  1149.  
  1150. #ifdef __STL_MEMBER_TEMPLATES
  1151.  
  1152. template <class _Tp, class _Traits, class _Alloc> 
  1153. template <class _InputIterator>
  1154. basic_string<_Tp, _Traits, _Alloc>& 
  1155. basic_string<_Tp, _Traits, _Alloc>::append(_InputIterator __first, 
  1156.                                           _InputIterator __last,
  1157.                                           input_iterator_tag) {
  1158.   for ( ; __first != __last ; ++__first)
  1159.     push_back(*__first);
  1160.   return *this;
  1161. }
  1162.  
  1163. template <class _Tp, class _Traits, class _Alloc> 
  1164. template <class _ForwardIter>
  1165. basic_string<_Tp, _Traits, _Alloc>& 
  1166. basic_string<_Tp, _Traits, _Alloc>::append(_ForwardIter __first, 
  1167.                                            _ForwardIter __last,
  1168.                                            forward_iterator_tag) {
  1169.   if (__first != __last) {
  1170.     const size_type __old_size = size();
  1171.     difference_type __n = 0;
  1172.     distance(__first, __last, __n);
  1173.     if (static_cast<size_type>(__n) > max_size() ||
  1174.         __old_size > max_size() - static_cast<size_type>(__n))
  1175.       _M_throw_length_error();
  1176.     if (__old_size + static_cast<size_type>(__n) > capacity()) {
  1177.       const size_type __len = __old_size +
  1178.                             max(__old_size, static_cast<size_type>(__n)) + 1;
  1179.       pointer __new_start = _M_allocate(__len);
  1180.       pointer __new_finish = __new_start;
  1181.       __STL_TRY {
  1182.         __new_finish = uninitialized_copy(_M_start, _M_finish, __new_start);
  1183.         __new_finish = uninitialized_copy(__first, __last, __new_finish);
  1184.         _M_construct_null(__new_finish);
  1185.       }
  1186.       __STL_UNWIND((destroy(__new_start,__new_finish),
  1187.                     _M_deallocate(__new_start,__len)));
  1188.       destroy(_M_start, _M_finish + 1);
  1189.       _M_deallocate_block();
  1190.       _M_start = __new_start;
  1191.       _M_finish = __new_finish;
  1192.       _M_end_of_storage = __new_start + __len; 
  1193.     }
  1194.     else {
  1195.       _ForwardIter __f1 = __first;
  1196.       ++__f1;
  1197.       uninitialized_copy(__f1, __last, _M_finish + 1);
  1198.       __STL_TRY {
  1199.         _M_construct_null(_M_finish + __n);
  1200.       }
  1201.       __STL_UNWIND(destroy(_M_finish + 1, _M_finish + __n));
  1202.       _Traits::assign(*_M_finish, *__first);
  1203.       _M_finish += __n;
  1204.     }
  1205.   }
  1206.   return *this;  
  1207. }
  1208.  
  1209. #else /* __STL_MEMBER_TEMPLATES */
  1210.  
  1211. template <class _Tp, class _Traits, class _Alloc> 
  1212. basic_string<_Tp, _Traits, _Alloc>& 
  1213. basic_string<_Tp, _Traits, _Alloc>::append(const _Tp* __first,
  1214.                                            const _Tp* __last)
  1215. {
  1216.   if (__first != __last) {
  1217.     const size_type __old_size = size();
  1218.     ptrdiff_t __n = __last - __first;
  1219.     if (__n > max_size() || __old_size > max_size() - __n)
  1220.       _M_throw_length_error();
  1221.     if (__old_size + __n > capacity()) {
  1222.       const size_type __len = __old_size + max(__old_size, (size_t) __n) + 1;
  1223.       pointer __new_start = _M_allocate(__len);
  1224.       pointer __new_finish = __new_start;
  1225.       __STL_TRY {
  1226.         __new_finish = uninitialized_copy(_M_start, _M_finish, __new_start);
  1227.         __new_finish = uninitialized_copy(__first, __last, __new_finish);
  1228.         _M_construct_null(__new_finish);
  1229.       }
  1230.       __STL_UNWIND((destroy(__new_start,__new_finish),
  1231.                     _M_deallocate(__new_start,__len)));
  1232.       destroy(_M_start, _M_finish + 1);
  1233.       _M_deallocate_block();
  1234.       _M_start = __new_start;
  1235.       _M_finish = __new_finish;
  1236.       _M_end_of_storage = __new_start + __len; 
  1237.     }
  1238.     else {
  1239.       const _Tp* __f1 = __first;
  1240.       ++__f1;
  1241.       uninitialized_copy(__f1, __last, _M_finish + 1);
  1242.       __STL_TRY {
  1243.         _M_construct_null(_M_finish + __n);
  1244.       }
  1245.       __STL_UNWIND(destroy(_M_finish + 1, _M_finish + __n));
  1246.       _Traits::assign(*_M_finish, *__first);
  1247.       _M_finish += __n;
  1248.     }
  1249.   }
  1250.   return *this;  
  1251. }
  1252.  
  1253. #endif /* __STL_MEMBER_TEMPLATES */
  1254.  
  1255. template <class _CharT, class _Traits, class _Alloc> 
  1256. basic_string<_CharT,_Traits,_Alloc>& 
  1257. basic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
  1258.   if (__n <= size()) {
  1259.     _Traits::assign(_M_start, __n, __c);
  1260.     erase(_M_start + __n, _M_finish);
  1261.   }
  1262.   else {
  1263.     _Traits::assign(_M_start, size(), __c);
  1264.     append(__n - size(), __c);
  1265.   }
  1266.   return *this;
  1267. }
  1268.  
  1269. #ifdef __STL_MEMBER_TEMPLATES
  1270.  
  1271. template <class _CharT, class _Traits, class _Alloc> 
  1272. template <class _InputIter>
  1273. basic_string<_CharT,_Traits,_Alloc>& basic_string<_CharT,_Traits,_Alloc>
  1274.   ::_M_assign_dispatch(_InputIter __f, _InputIter __l, __false_type)
  1275. {
  1276.   pointer __cur = _M_start;
  1277.   while (__f != __l && __cur != _M_finish) {
  1278.     _Traits::assign(*__cur, *__f);
  1279.     ++__f;
  1280.     ++__cur;
  1281.   }
  1282.   if (__f == __l)
  1283.     erase(__cur, _M_finish);
  1284.   else
  1285.     append(__f, __l);
  1286.   return *this;
  1287. }
  1288.  
  1289. #endif /* __STL_MEMBER_TEMPLATES */
  1290.  
  1291. template <class _CharT, class _Traits, class _Alloc> 
  1292. basic_string<_CharT,_Traits,_Alloc>& 
  1293. basic_string<_CharT,_Traits,_Alloc>::assign(const _CharT* __f, 
  1294.                                             const _CharT* __l)
  1295. {
  1296.   const ptrdiff_t __n = __l - __f;
  1297.   if (static_cast<size_type>(__n) <= size()) {
  1298.     _Traits::copy(_M_start, __f, __n);
  1299.     erase(_M_start + __n, _M_finish);
  1300.   }
  1301.   else {
  1302.     _Traits::copy(_M_start, __f, size());
  1303.     append(__f + size(), __l);
  1304.   }
  1305.   return *this;
  1306. }
  1307.  
  1308. template <class _CharT, class _Traits, class _Alloc>
  1309. basic_string<_CharT,_Traits,_Alloc>::iterator 
  1310. basic_string<_CharT,_Traits,_Alloc>
  1311.   ::_M_insert_aux(basic_string<_CharT,_Traits,_Alloc>::iterator __p,
  1312.                   _CharT __c)
  1313. {
  1314.   iterator __new_pos = __p;
  1315.   if (_M_finish + 1 < _M_end_of_storage) {
  1316.     _M_construct_null(_M_finish + 1);
  1317.     _Traits::move(__p + 1, __p, _M_finish - __p);
  1318.     _Traits::assign(*__p, __c);
  1319.     ++_M_finish;
  1320.   }
  1321.   else {
  1322.     const size_type __old_len = size();
  1323.     const size_type __len = __old_len +
  1324.                             max(__old_len, static_cast<size_type>(1)) + 1;
  1325.     iterator __new_start = _M_allocate(__len);
  1326.     iterator __new_finish = __new_start;
  1327.     __STL_TRY {
  1328.       __new_pos = uninitialized_copy(_M_start, __p, __new_start);
  1329.       construct(__new_pos, __c);
  1330.       __new_finish = __new_pos + 1;
  1331.       __new_finish = uninitialized_copy(__p, _M_finish, __new_finish);
  1332.       _M_construct_null(__new_finish);
  1333.     }
  1334.     __STL_UNWIND((destroy(__new_start,__new_finish), 
  1335.                   _M_deallocate(__new_start,__len)));
  1336.     destroy(_M_start, _M_finish + 1);
  1337.     _M_deallocate_block();
  1338.     _M_start = __new_start;
  1339.     _M_finish = __new_finish;
  1340.     _M_end_of_storage = __new_start + __len;
  1341.   }
  1342.   return __new_pos;
  1343. }
  1344.  
  1345. template <class _CharT, class _Traits, class _Alloc>
  1346. void basic_string<_CharT,_Traits,_Alloc>
  1347.   ::insert(basic_string<_CharT,_Traits,_Alloc>::iterator __position,
  1348.            size_t __n, _CharT __c)
  1349. {
  1350.   if (__n != 0) {
  1351.     if (size_type(_M_end_of_storage - _M_finish) >= __n + 1) {
  1352.       const size_type __elems_after = _M_finish - __position;
  1353.       iterator __old_finish = _M_finish;
  1354.       if (__elems_after >= __n) {
  1355.         uninitialized_copy((_M_finish - __n) + 1, _M_finish + 1,
  1356.                            _M_finish + 1);
  1357.         _M_finish += __n;
  1358.         _Traits::move(__position + __n,
  1359.                       __position, (__elems_after - __n) + 1);
  1360.         _Traits::assign(__position, __n, __c);
  1361.       }
  1362.       else {
  1363.         uninitialized_fill_n(_M_finish + 1, __n - __elems_after - 1, __c);
  1364.         _M_finish += __n - __elems_after;
  1365.         __STL_TRY {
  1366.           uninitialized_copy(__position, __old_finish + 1, _M_finish);
  1367.           _M_finish += __elems_after;
  1368.         }
  1369.         __STL_UNWIND((destroy(__old_finish + 1, _M_finish), 
  1370.                       _M_finish = __old_finish));
  1371.         _Traits::assign(__position, __elems_after + 1, __c);
  1372.       }
  1373.     }
  1374.     else {
  1375.       const size_type __old_size = size();        
  1376.       const size_type __len = __old_size + max(__old_size, __n) + 1;
  1377.       iterator __new_start = _M_allocate(__len);
  1378.       iterator __new_finish = __new_start;
  1379.       __STL_TRY {
  1380.         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
  1381.         __new_finish = uninitialized_fill_n(__new_finish, __n, __c);
  1382.         __new_finish = uninitialized_copy(__position, _M_finish,
  1383.                                           __new_finish);
  1384.         _M_construct_null(__new_finish);
  1385.       }
  1386.       __STL_UNWIND((destroy(__new_start,__new_finish),
  1387.                     _M_deallocate(__new_start,__len)));
  1388.       destroy(_M_start, _M_finish + 1);
  1389.       _M_deallocate_block();
  1390.       _M_start = __new_start;
  1391.       _M_finish = __new_finish;
  1392.       _M_end_of_storage = __new_start + __len;    
  1393.     }
  1394.   }
  1395. }
  1396.  
  1397. #ifdef __STL_MEMBER_TEMPLATES
  1398.  
  1399. template <class _Tp, class _Traits, class _Alloc>
  1400. template <class _InputIter>
  1401. void basic_string<_Tp, _Traits, _Alloc>::insert(iterator __p,
  1402.                                                 _InputIter __first, 
  1403.                                                 _InputIter __last,
  1404.                                                 input_iterator_tag)
  1405. {
  1406.   for ( ; __first != __last; ++__first) {
  1407.     __p = insert(__p, *__first);
  1408.     ++__p;
  1409.   }
  1410. }
  1411.  
  1412. template <class _CharT, class _Traits, class _Alloc>
  1413. template <class _ForwardIter>
  1414. void 
  1415. basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
  1416.                                             _ForwardIter __first, 
  1417.                                             _ForwardIter __last,
  1418.                                             forward_iterator_tag)
  1419. {
  1420.   if (__first != __last) {
  1421.     difference_type __n = 0;
  1422.     distance(__first, __last, __n);
  1423.     if (_M_end_of_storage - _M_finish >= __n + 1) {
  1424.       const difference_type __elems_after = _M_finish - __position;
  1425.       iterator __old_finish = _M_finish;
  1426.       if (__elems_after >= __n) {
  1427.         uninitialized_copy((_M_finish - __n) + 1, _M_finish + 1,
  1428.                            _M_finish + 1);
  1429.         _M_finish += __n;
  1430.         _Traits::move(__position + __n,
  1431.                       __position, (__elems_after - __n) + 1);
  1432.         _M_copy(__first, __last, __position);
  1433.       }
  1434.       else {
  1435.         _ForwardIter __mid = __first;
  1436.         advance(__mid, __elems_after + 1);
  1437.         uninitialized_copy(__mid, __last, _M_finish + 1);
  1438.         _M_finish += __n - __elems_after;
  1439.         __STL_TRY {
  1440.           uninitialized_copy(__position, __old_finish + 1, _M_finish);
  1441.           _M_finish += __elems_after;
  1442.         }
  1443.         __STL_UNWIND((destroy(__old_finish + 1, _M_finish), 
  1444.                       _M_finish = __old_finish));
  1445.         _M_copy(__first, __mid, __position);
  1446.       }
  1447.     }
  1448.     else {
  1449.       const size_type __old_size = size();        
  1450.       const size_type __len
  1451.         = __old_size + max(__old_size, static_cast<size_type>(__n)) + 1;
  1452.       pointer __new_start = _M_allocate(__len);
  1453.       pointer __new_finish = __new_start;
  1454.       __STL_TRY {
  1455.         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
  1456.         __new_finish = uninitialized_copy(__first, __last, __new_finish);
  1457.         __new_finish
  1458.           = uninitialized_copy(__position, _M_finish, __new_finish);
  1459.         _M_construct_null(__new_finish);
  1460.       }
  1461.       __STL_UNWIND((destroy(__new_start,__new_finish),
  1462.                     _M_deallocate(__new_start,__len)));
  1463.       destroy(_M_start, _M_finish + 1);
  1464.       _M_deallocate_block();
  1465.       _M_start = __new_start;
  1466.       _M_finish = __new_finish;
  1467.       _M_end_of_storage = __new_start + __len; 
  1468.     }
  1469.   }
  1470. }
  1471.  
  1472. #else /* __STL_MEMBER_TEMPLATES */
  1473.  
  1474. template <class _CharT, class _Traits, class _Alloc>
  1475. void 
  1476. basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
  1477.                                             const _CharT* __first, 
  1478.                                             const _CharT* __last)
  1479. {
  1480.   if (__first != __last) {
  1481.     const ptrdiff_t __n = __last - __first;
  1482.     if (_M_end_of_storage - _M_finish >= __n + 1) {
  1483.       const ptrdiff_t __elems_after = _M_finish - __position;
  1484.       iterator __old_finish = _M_finish;
  1485.       if (__elems_after >= __n) {
  1486.         uninitialized_copy((_M_finish - __n) + 1, _M_finish + 1,
  1487.                            _M_finish + 1);
  1488.         _M_finish += __n;
  1489.         _Traits::move(__position + __n,
  1490.                       __position, (__elems_after - __n) + 1);
  1491.         _M_copy(__first, __last, __position);
  1492.       }
  1493.       else {
  1494.         const _CharT* __mid = __first;
  1495.         advance(__mid, __elems_after + 1);
  1496.         uninitialized_copy(__mid, __last, _M_finish + 1);
  1497.         _M_finish += __n - __elems_after;
  1498.         __STL_TRY {
  1499.           uninitialized_copy(__position, __old_finish + 1, _M_finish);
  1500.           _M_finish += __elems_after;
  1501.         }
  1502.         __STL_UNWIND((destroy(__old_finish + 1, _M_finish), 
  1503.                       _M_finish = __old_finish));
  1504.         _M_copy(__first, __mid, __position);
  1505.       }
  1506.     }
  1507.     else {
  1508.       const size_type __old_size = size();        
  1509.       const size_type __len
  1510.         = __old_size + max(__old_size, static_cast<size_type>(__n)) + 1;
  1511.       pointer __new_start = _M_allocate(__len);
  1512.       pointer __new_finish = __new_start;
  1513.       __STL_TRY {
  1514.         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
  1515.         __new_finish = uninitialized_copy(__first, __last, __new_finish);
  1516.         __new_finish
  1517.           = uninitialized_copy(__position, _M_finish, __new_finish);
  1518.         _M_construct_null(__new_finish);
  1519.       }
  1520.       __STL_UNWIND((destroy(__new_start,__new_finish),
  1521.                     _M_deallocate(__new_start,__len)));
  1522.       destroy(_M_start, _M_finish + 1);
  1523.       _M_deallocate_block();
  1524.       _M_start = __new_start;
  1525.       _M_finish = __new_finish;
  1526.       _M_end_of_storage = __new_start + __len; 
  1527.     }
  1528.   }
  1529. }
  1530.  
  1531. #endif /* __STL_MEMBER_TEMPLATES */
  1532.  
  1533. template <class _CharT, class _Traits, class _Alloc>
  1534. basic_string<_CharT,_Traits,_Alloc>&
  1535. basic_string<_CharT,_Traits,_Alloc>
  1536.   ::replace(iterator __first, iterator __last, size_type __n, _CharT __c)
  1537. {
  1538.   const size_type __len = static_cast<size_type>(__last - __first);
  1539.   if (__len >= __n) {
  1540.     _Traits::assign(__first, __n, __c);
  1541.     erase(__first + __n, __last);
  1542.   }
  1543.   else {
  1544.     _Traits::assign(__first, __len, __c);
  1545.     insert(__last, __n - __len, __c);
  1546.   }
  1547.   return *this;
  1548. }
  1549.  
  1550. #ifdef __STL_MEMBER_TEMPLATES
  1551.  
  1552. template <class _CharT, class _Traits, class _Alloc>
  1553. template <class _InputIter>
  1554. basic_string<_CharT,_Traits,_Alloc>&
  1555. basic_string<_CharT,_Traits,_Alloc>
  1556.   ::replace(iterator __first, iterator __last, _InputIter __f, _InputIter __l,
  1557.             input_iterator_tag) 
  1558. {
  1559.   for ( ; __first != __last && __f != __l; ++__first, ++__f)
  1560.     _Traits::assign(*__first, *__f);
  1561.  
  1562.   if (__f == __l)
  1563.     erase(__first, __last);
  1564.   else
  1565.     insert(__last, __f, __l);
  1566.   return *this;
  1567. }
  1568.  
  1569. template <class _CharT, class _Traits, class _Alloc>
  1570. template <class _ForwardIter>
  1571. basic_string<_CharT,_Traits,_Alloc>&
  1572. basic_string<_CharT,_Traits,_Alloc>
  1573.   ::replace(iterator __first, iterator __last,
  1574.             _ForwardIter __f, _ForwardIter __l,
  1575.             forward_iterator_tag) 
  1576. {
  1577.   difference_type __n = 0;
  1578.   distance(__f, __l, __n);
  1579.   const difference_type __len = __last - __first;
  1580.   if (__len >= __n) {
  1581.     _M_copy(__f, __l, __first);
  1582.     erase(__first + __n, __last);
  1583.   }
  1584.   else {
  1585.     _ForwardIter __m = __f;
  1586.     advance(__m, __len);
  1587.     _M_copy(__f, __m, __first);
  1588.     insert(__last, __m, __l);
  1589.   }
  1590.   return *this;
  1591. }
  1592.  
  1593. #else /* __STL_MEMBER_TEMPLATES */
  1594.  
  1595. template <class _CharT, class _Traits, class _Alloc>
  1596. basic_string<_CharT,_Traits,_Alloc>&
  1597. basic_string<_CharT,_Traits,_Alloc>
  1598.   ::replace(iterator __first, iterator __last,
  1599.             const _CharT* __f, const _CharT* __l)
  1600. {
  1601.   const ptrdiff_t         __n = __l - __f;
  1602.   const difference_type __len = __last - __first;
  1603.   if (__len >= __n) {
  1604.     _M_copy(__f, __l, __first);
  1605.     erase(__first + __n, __last);
  1606.   }
  1607.   else {
  1608.     const _CharT* __m = __f + __len;
  1609.     _M_copy(__f, __m, __first);
  1610.     insert(__last, __m, __l);
  1611.   }
  1612.   return *this;
  1613. }
  1614.  
  1615. #endif /* __STL_MEMBER_TEMPLATES */
  1616.  
  1617. template <class _CharT, class _Traits, class _Alloc>
  1618. basic_string<_CharT,_Traits,_Alloc>::size_type
  1619. basic_string<_CharT,_Traits,_Alloc>
  1620.   ::find(const _CharT* __s, size_type __pos, size_type __n) const 
  1621. {
  1622.   if (__pos + __n > size())
  1623.     return npos;
  1624.   else {
  1625.     const const_iterator __result =
  1626.       search(_M_start + __pos, _M_finish, 
  1627.              __s, __s + __n, _Eq_traits<_Traits>());
  1628.     return __result != _M_finish ? __result - begin() : npos;
  1629.   }
  1630. }
  1631.  
  1632. template <class _CharT, class _Traits, class _Alloc>
  1633. basic_string<_CharT,_Traits,_Alloc>::size_type
  1634. basic_string<_CharT,_Traits,_Alloc>
  1635.   ::find(_CharT __c, size_type __pos) const 
  1636. {
  1637.   if (__pos >= size())
  1638.     return npos;
  1639.   else {
  1640.     const const_iterator __result =
  1641.       find_if(_M_start + __pos, _M_finish,
  1642.               bind2nd(_Eq_traits<_Traits>(), __c));
  1643.     return __result != _M_finish ? __result - begin() : npos;
  1644.   }
  1645. }    
  1646.  
  1647. template <class _CharT, class _Traits, class _Alloc>
  1648. basic_string<_CharT,_Traits,_Alloc>::size_type
  1649. basic_string<_CharT,_Traits,_Alloc>
  1650.   ::rfind(const _CharT* __s, size_type __pos, size_type __n) const 
  1651. {
  1652.   const size_t __len = size();
  1653.  
  1654.   if (__n > __len)
  1655.     return npos;
  1656.   else if (__n == 0)
  1657.     return min(__len, __pos);
  1658.   else {
  1659.     const const_iterator __last = begin() + min(__len - __n, __pos) + __n;
  1660.     const const_iterator __result = find_end(begin(), __last,
  1661.                                            __s, __s + __n,
  1662.                                            _Eq_traits<_Traits>());
  1663.     return __result != __last ? __result - begin() : npos;
  1664.   }
  1665. }
  1666.  
  1667. template <class _CharT, class _Traits, class _Alloc>
  1668. basic_string<_CharT,_Traits,_Alloc>::size_type
  1669. basic_string<_CharT,_Traits,_Alloc>
  1670.   ::rfind(_CharT __c, size_type __pos) const 
  1671. {
  1672.   const size_type __len = size();
  1673.  
  1674.   if (__len < 1)
  1675.     return npos;
  1676.   else {
  1677.     const const_iterator __last = begin() + min(__len - 1, __pos) + 1;
  1678.     const_reverse_iterator __rresult =
  1679.       find_if(const_reverse_iterator(__last), rend(),
  1680.               bind2nd(_Eq_traits<_Traits>(), __c));
  1681.     return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  1682.   }
  1683. }
  1684.  
  1685. template <class _CharT, class _Traits, class _Alloc>
  1686. basic_string<_CharT,_Traits,_Alloc>::size_type
  1687. basic_string<_CharT,_Traits,_Alloc>
  1688.   ::find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  1689. {
  1690.   if (__pos >= size())
  1691.     return npos;
  1692.   else {
  1693.     const_iterator __result = __STD::find_first_of(begin() + __pos, end(),
  1694.                                                    __s, __s + __n,
  1695.                                                    _Eq_traits<_Traits>());
  1696.     return __result != _M_finish ? __result - begin() : npos;
  1697.   }
  1698. }
  1699.  
  1700.  
  1701. template <class _CharT, class _Traits, class _Alloc>
  1702. basic_string<_CharT,_Traits,_Alloc>::size_type
  1703. basic_string<_CharT,_Traits,_Alloc>
  1704.   ::find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  1705. {
  1706.   const size_type __len = size();
  1707.  
  1708.   if (__len < 1)
  1709.     return npos;
  1710.   else {
  1711.     const const_iterator __last = _M_start + min(__len - 1, __pos) + 1;
  1712.     const const_reverse_iterator __rresult =
  1713.       __STD::find_first_of(const_reverse_iterator(__last), rend(),
  1714.                            __s, __s + __n,
  1715.                            _Eq_traits<_Traits>());
  1716.     return __rresult != rend() ? (__rresult.base() - 1) - _M_start : npos;
  1717.   }
  1718. }
  1719.  
  1720.  
  1721. template <class _CharT, class _Traits, class _Alloc>
  1722. basic_string<_CharT,_Traits,_Alloc>::size_type
  1723. basic_string<_CharT,_Traits,_Alloc>
  1724.   ::find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  1725. {
  1726.   if (__pos > size())
  1727.     return npos;
  1728.   else {
  1729.     const_iterator __result = find_if(_M_start + __pos, _M_finish,
  1730.                                 _Not_within_traits<_Traits>(__s, __s + __n));
  1731.     return __result != _M_finish ? __result - _M_start : npos;
  1732.   }
  1733. }
  1734.  
  1735. template <class _CharT, class _Traits, class _Alloc>
  1736. basic_string<_CharT,_Traits,_Alloc>::size_type
  1737. basic_string<_CharT,_Traits,_Alloc>
  1738.   ::find_first_not_of(_CharT __c, size_type __pos) const
  1739. {
  1740.   if (__pos > size())
  1741.     return npos;
  1742.   else {
  1743.     const_iterator __result
  1744.       = find_if(begin() + __pos, end(),
  1745.                 not1(bind2nd(_Eq_traits<_Traits>(), __c)));
  1746.     return __result != _M_finish ? __result - begin() : npos;
  1747.   }
  1748. }    
  1749.  
  1750. template <class _CharT, class _Traits, class _Alloc>
  1751. basic_string<_CharT,_Traits,_Alloc>::size_type
  1752. basic_string<_CharT,_Traits,_Alloc>
  1753.   ::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const 
  1754. {
  1755.  
  1756.   const size_type __len = size();
  1757.  
  1758.   if (__len < 1)
  1759.     return npos;
  1760.   else {
  1761.     const const_iterator __last = begin() + min(__len - 1, __pos) + 1;
  1762.     const const_reverse_iterator __rresult =
  1763.       find_if(const_reverse_iterator(__last), rend(),
  1764.               _Not_within_traits<_Traits>(__s, __s + __n));
  1765.     return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  1766.   }
  1767. }
  1768.  
  1769. template <class _Tp, class _Traits, class _Alloc>
  1770. basic_string<_Tp, _Traits, _Alloc>::size_type
  1771. basic_string<_Tp, _Traits, _Alloc>
  1772.   ::find_last_not_of(_Tp __c, size_type __pos) const 
  1773. {
  1774.   const size_type __len = size();
  1775.  
  1776.   if (__len < 1)
  1777.     return npos;
  1778.   else {
  1779.     const const_iterator __last = begin() + min(__len - 1, __pos) + 1;
  1780.     const_reverse_iterator __rresult =
  1781.       find_if(const_reverse_iterator(__last), rend(),
  1782.               not1(bind2nd(_Eq_traits<_Traits>(), __c)));
  1783.     return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  1784.   }
  1785. }
  1786.  
  1787. // ------------------------------------------------------------
  1788. // Non-member functions.
  1789.  
  1790. // Operator+
  1791.  
  1792. template <class _CharT, class _Traits, class _Alloc>
  1793. inline basic_string<_CharT,_Traits,_Alloc>
  1794. operator+(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1795.           const basic_string<_CharT,_Traits,_Alloc>& __y)
  1796. {
  1797.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1798.   typedef typename _Str::_Reserve_t _Reserve_t;
  1799.   _Reserve_t __reserve;
  1800.   _Str __result(__reserve, __x.size() + __y.size(), __x.get_allocator());
  1801.   __result.append(__x);
  1802.   __result.append(__y);
  1803.   return __result;
  1804. }
  1805.  
  1806. template <class _CharT, class _Traits, class _Alloc>
  1807. inline basic_string<_CharT,_Traits,_Alloc>
  1808. operator+(const _CharT* __s,
  1809.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1810.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1811.   typedef typename _Str::_Reserve_t _Reserve_t;
  1812.   _Reserve_t __reserve;
  1813.   const size_t __n = _Traits::length(__s);
  1814.   _Str __result(__reserve, __n + __y.size());
  1815.   __result.append(__s, __s + __n);
  1816.   __result.append(__y);
  1817.   return __result;
  1818. }
  1819.  
  1820. template <class _CharT, class _Traits, class _Alloc>
  1821. inline basic_string<_CharT,_Traits,_Alloc>
  1822. operator+(_CharT __c,
  1823.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1824.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1825.   typedef typename _Str::_Reserve_t _Reserve_t;
  1826.   _Reserve_t __reserve;
  1827.   _Str __result(__reserve, 1 + __y.size());
  1828.   __result.push_back(__c);
  1829.   __result.append(__y);
  1830.   return __result;
  1831. }
  1832.  
  1833. template <class _CharT, class _Traits, class _Alloc>
  1834. inline basic_string<_CharT,_Traits,_Alloc>
  1835. operator+(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1836.           const _CharT* __s) {
  1837.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1838.   typedef typename _Str::_Reserve_t _Reserve_t;
  1839.   _Reserve_t __reserve;
  1840.   const size_t __n = _Traits::length(__s);
  1841.   _Str __result(__reserve, __x.size() + __n, __x.get_allocator());
  1842.   __result.append(__x);
  1843.   __result.append(__s, __s + __n);
  1844.   return __result;
  1845. }
  1846.  
  1847. template <class _CharT, class _Traits, class _Alloc>
  1848. inline basic_string<_CharT,_Traits,_Alloc>
  1849. operator+(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1850.           const _CharT __c) {
  1851.   typedef basic_string<_CharT,_Traits,_Alloc> _Str;
  1852.   typedef typename _Str::_Reserve_t _Reserve_t;
  1853.   _Reserve_t __reserve;
  1854.   _Str __result(__reserve, __x.size() + 1, __x.get_allocator());
  1855.   __result.append(__x);
  1856.   __result.push_back(__c);
  1857.   return __result;
  1858. }
  1859.  
  1860. // Operator== and operator!=
  1861.  
  1862. template <class _CharT, class _Traits, class _Alloc>
  1863. inline bool
  1864. operator==(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1865.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1866.   return __x.size() == __y.size() &&
  1867.          _Traits::compare(__x.data(), __y.data(), __x.size()) == 0;
  1868. }
  1869.  
  1870. template <class _CharT, class _Traits, class _Alloc>
  1871. inline bool
  1872. operator==(const _CharT* __s,
  1873.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1874.   size_t __n = _Traits::length(__s);
  1875.   return __n == __y.size() && _Traits::compare(__s, __y.data(), __n) == 0;
  1876. }
  1877.  
  1878. template <class _CharT, class _Traits, class _Alloc>
  1879. inline bool
  1880. operator==(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1881.            const _CharT* __s) {
  1882.   size_t __n = _Traits::length(__s);
  1883.   return __x.size() == __n && _Traits::compare(__x.data(), __s, __n) == 0;
  1884. }
  1885.  
  1886. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  1887.  
  1888. template <class _CharT, class _Traits, class _Alloc>
  1889. inline bool
  1890. operator!=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1891.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1892.   return !(__x == __y);
  1893. }
  1894.  
  1895. template <class _CharT, class _Traits, class _Alloc>
  1896. inline bool
  1897. operator!=(const _CharT* __s,
  1898.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1899.   return !(__s == __y);
  1900. }
  1901.  
  1902. template <class _CharT, class _Traits, class _Alloc>
  1903. inline bool
  1904. operator!=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1905.            const _CharT* __s) {
  1906.   return !(__x == __s);
  1907. }
  1908.  
  1909. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  1910.  
  1911. // Operator< (and also >, <=, and >=).
  1912.  
  1913. template <class _CharT, class _Traits, class _Alloc>
  1914. inline bool
  1915. operator<(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1916.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1917.   return basic_string<_CharT,_Traits,_Alloc>
  1918.     ::_M_compare(__x.begin(), __x.end(), __y.begin(), __y.end()) < 0;
  1919. }
  1920.  
  1921. template <class _CharT, class _Traits, class _Alloc>
  1922. inline bool
  1923. operator<(const _CharT* __s,
  1924.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1925.   size_t __n = _Traits::length(__s);
  1926.   return basic_string<_CharT,_Traits,_Alloc>
  1927.     ::_M_compare(__s, __s + __n, __y.begin(), __y.end()) < 0;
  1928. }
  1929.  
  1930. template <class _CharT, class _Traits, class _Alloc>
  1931. inline bool
  1932. operator<(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1933.           const _CharT* __s) {
  1934.   size_t __n = _Traits::length(__s);
  1935.   return basic_string<_CharT,_Traits,_Alloc>
  1936.     ::_M_compare(__x.begin(), __x.end(), __s, __s + __n) < 0;
  1937. }
  1938.  
  1939. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  1940.  
  1941. template <class _CharT, class _Traits, class _Alloc>
  1942. inline bool
  1943. operator>(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1944.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1945.   return __y < __x;
  1946. }
  1947.  
  1948. template <class _CharT, class _Traits, class _Alloc>
  1949. inline bool
  1950. operator>(const _CharT* __s,
  1951.           const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1952.   return __y < __s;
  1953. }
  1954.  
  1955. template <class _CharT, class _Traits, class _Alloc>
  1956. inline bool
  1957. operator>(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1958.           const _CharT* __s) {
  1959.   return __s < __x;
  1960. }
  1961.  
  1962. template <class _CharT, class _Traits, class _Alloc>
  1963. inline bool
  1964. operator<=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1965.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1966.   return !(__y < __x);
  1967. }
  1968.  
  1969. template <class _CharT, class _Traits, class _Alloc>
  1970. inline bool
  1971. operator<=(const _CharT* __s,
  1972.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1973.   return !(__y < __s);
  1974. }
  1975.  
  1976. template <class _CharT, class _Traits, class _Alloc>
  1977. inline bool
  1978. operator<=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1979.            const _CharT* __s) {
  1980.   return !(__s < __x);
  1981. }
  1982.  
  1983. template <class _CharT, class _Traits, class _Alloc>
  1984. inline bool
  1985. operator>=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  1986.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1987.   return !(__x < __y);
  1988. }
  1989.  
  1990. template <class _CharT, class _Traits, class _Alloc>
  1991. inline bool
  1992. operator>=(const _CharT* __s,
  1993.            const basic_string<_CharT,_Traits,_Alloc>& __y) {
  1994.   return !(__s < __y);
  1995. }
  1996.  
  1997. template <class _CharT, class _Traits, class _Alloc>
  1998. inline bool
  1999. operator>=(const basic_string<_CharT,_Traits,_Alloc>& __x,
  2000.            const _CharT* __s) {
  2001.   return !(__x < __s);
  2002. }
  2003.  
  2004. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  2005.  
  2006. // Swap.
  2007.  
  2008. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  2009.  
  2010. template <class _CharT, class _Traits, class _Alloc>
  2011. inline void swap(basic_string<_CharT,_Traits,_Alloc>& __x,
  2012.                  basic_string<_CharT,_Traits,_Alloc>& __y) {
  2013.   __x.swap(__y);
  2014. }
  2015.  
  2016. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  2017.  
  2018. // I/O.  
  2019.  
  2020. #ifndef __STL_USE_NEW_IOSTREAMS 
  2021. __STL_END_NAMESPACE
  2022. #include <iostream.h>
  2023. __STL_BEGIN_NAMESPACE
  2024. #endif /* __STL_USE_NEW_IOSTREAMS */
  2025.  
  2026. #ifdef __STL_USE_NEW_IOSTREAMS
  2027.  
  2028. template <class _CharT, class _Traits>
  2029. inline bool
  2030. __sgi_string_fill(basic_ostream<_CharT, _Traits>& __os,
  2031.                   basic_streambuf<_CharT, _Traits>* __buf,
  2032.                   size_t __n)
  2033. {
  2034.   _CharT __f = __os.fill();
  2035.   size_t __i;
  2036.   bool __ok = true;
  2037.  
  2038.   for (__i = 0; __i < __n; __i++)
  2039.     __ok = __ok && !_Traits::eq_int_type(__buf->sputc(__f), _Traits::eof());
  2040.   return __ok;
  2041. }
  2042.  
  2043. template <class _CharT, class _Traits, class _Alloc>
  2044. basic_ostream<_CharT, _Traits>&
  2045. operator<<(basic_ostream<_CharT, _Traits>& __os, 
  2046.            const basic_string<_CharT,_Traits,_Alloc>& __s)
  2047. {
  2048.   typename basic_ostream<_CharT, _Traits>::sentry __sentry(__os);
  2049.   bool __ok = false;
  2050.  
  2051.   if (__sentry) {
  2052.     __ok = true;
  2053.     size_t __n = __s.size();
  2054.     size_t __pad_len = 0;
  2055.     const bool __left = (__os.flags() & ios::left) != 0;
  2056.     const size_t __w = __os.width(0);
  2057.     basic_streambuf<_CharT, _Traits>* __buf = __os.rdbuf();
  2058.  
  2059.     if (__w != 0 && __n < __w)
  2060.       __pad_len = __w - __n;
  2061.     
  2062.     if (!__left)
  2063.       __ok = __sgi_string_fill(__os, __buf, __pad_len);    
  2064.  
  2065.     __ok = __ok && 
  2066.            __buf->sputn(__s.data(), streamsize(__n)) == streamsize(__n);
  2067.  
  2068.     if (__left)
  2069.       __ok = __ok && __sgi_string_fill(__os, __buf, __pad_len);
  2070.   }
  2071.  
  2072.   if (!__ok)
  2073.     __os.setstate(ios_base::failbit);
  2074.  
  2075.   return __os;
  2076. }
  2077.  
  2078. template <class _CharT, class _Traits, class _Alloc>
  2079. basic_istream<_CharT, _Traits>& 
  2080. operator>>(basic_istream<_CharT, _Traits>& __is,
  2081.            basic_string<_CharT,_Traits,_Alloc>& __s)
  2082. {
  2083.   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
  2084.  
  2085.   if (__sentry) {
  2086.     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
  2087.     const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__is.getloc());
  2088.  
  2089.     __s.clear();
  2090.     size_t __n = __is.width(0);
  2091.     if (__n == 0)
  2092.       __n = static_cast<size_t>(-1);
  2093.     else
  2094.       __s.reserve(__n);
  2095.     
  2096.  
  2097.     while (__n-- > 0) {
  2098.       typename _Traits::int_type __c1 = __buf->sbumpc();
  2099.       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
  2100.         __is.setstate(ios_base::eofbit);
  2101.         break;
  2102.       }
  2103.       else {
  2104.         _CharT __c = _Traits::to_char_type(__c1);
  2105.  
  2106.         if (__ctype.is(ctype<_CharT>::space, __c)) {
  2107.           if (_Traits::eq_int_type(__buf->sputbackc(__c), _Traits::eof()))
  2108.             __is.setstate(ios_base::failbit);
  2109.           break;
  2110.         }
  2111.         else
  2112.           __s.push_back(__c);
  2113.       }
  2114.     }
  2115.     
  2116.     // If we have read no characters, then set failbit.
  2117.     if (__s.size() == 0)
  2118.       __is.setstate(ios_base::failbit);
  2119.   }
  2120.   else
  2121.     __is.setstate(ios_base::failbit);
  2122.  
  2123.   return __is;
  2124. }
  2125.  
  2126. template <class _CharT, class _Traits, class _Alloc>    
  2127. basic_istream<_CharT, _Traits>& 
  2128. getline(istream& __is,
  2129.         basic_string<_CharT,_Traits,_Alloc>& __s,
  2130.         _CharT __delim)
  2131. {
  2132.   size_t __nread = 0;
  2133.   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is, true);
  2134.   if (__sentry) {
  2135.     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
  2136.     __s.clear();
  2137.  
  2138.     int __c1;
  2139.     while (__nread < __s.max_size()) {
  2140.       int __c1 = __buf->sbumpc();
  2141.       if (_Traits::eq_int_type(__c1, _Traits::eof())) {
  2142.         __is.setstate(ios_base::eofbit);
  2143.         break;
  2144.       }
  2145.       else {
  2146.         ++__nread;
  2147.         _CharT __c = _Traits::to_char_type(__c1);
  2148.         if (!_Traits::eq(__c, __delim)) 
  2149.           __s.push_back(__c);
  2150.         else
  2151.           break;              // Character is extracted but not appended.
  2152.       }
  2153.     }
  2154.   }
  2155.   if (__nread == 0 || __nread >= __s.max_size())
  2156.     __is.setstate(ios_base::failbit);
  2157.  
  2158.   return __is;
  2159. }
  2160.  
  2161. template <class _CharT, class _Traits, class _Alloc>    
  2162. inline basic_istream<_CharT, _Traits>& 
  2163. getline(basic_istream<_CharT, _Traits>& __is,
  2164.         basic_string<_CharT,_Traits,_Alloc>& __s)
  2165. {
  2166.   return getline(__is, __s, '\n');
  2167. }
  2168.  
  2169. #else /* __STL_USE_NEW_IOSTREAMS */
  2170.  
  2171. inline void __sgi_string_fill(ostream& __os, streambuf* __buf, size_t __n)
  2172. {
  2173.   char __f = __os.fill();
  2174.   size_t __i;
  2175.  
  2176.   for (__i = 0; __i < __n; __i++) __buf->sputc(__f);
  2177. }
  2178.  
  2179. template <class _CharT, class _Traits, class _Alloc>
  2180. ostream& operator<<(ostream& __os, 
  2181.                     const basic_string<_CharT,_Traits,_Alloc>& __s)
  2182. {
  2183.   streambuf* __buf = __os.rdbuf();
  2184.   if (__buf) {
  2185.     size_t __n = __s.size();
  2186.     size_t __pad_len = 0;
  2187.     const bool __left = (__os.flags() & ios::left) != 0;
  2188.     const size_t __w = __os.width();
  2189.  
  2190.     if (__w > 0) {
  2191.       __n = min(__w, __n);
  2192.       __pad_len = __w - __n;
  2193.     }
  2194.     
  2195.     if (!__left)
  2196.       __sgi_string_fill(__os, __buf, __pad_len);
  2197.   
  2198.     const size_t __nwritten = __buf->sputn(__s.data(), __n);
  2199.  
  2200.     if (__left)
  2201.       __sgi_string_fill(__os, __buf, __pad_len);
  2202.  
  2203.     if (__nwritten != __n)
  2204.       __os.clear(__os.rdstate() | ios::failbit);
  2205.  
  2206.     __os.width(0);
  2207.   }
  2208.   else
  2209.     __os.clear(__os.rdstate() | ios::badbit);
  2210.  
  2211.   return __os;
  2212. }
  2213.  
  2214. template <class _CharT, class _Traits, class _Alloc>
  2215. istream& operator>>(istream& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
  2216. {
  2217.   if (!__is)
  2218.     return __is;
  2219.  
  2220.   streambuf* __buf = __is.rdbuf();
  2221.   if (__buf) {
  2222.  
  2223. #ifdef __USLC__
  2224. /* Jochen Schlick '1999  - operator >> modified. Work-around to get the 
  2225.  *                         output buffer flushed (necessary when using 
  2226.  *                         "cout" (without endl or flushing) followed by
  2227.  *                         "cin >>" ...)
  2228.  */
  2229.     if (__is.flags() & ios::skipws) {
  2230.       _CharT __c;
  2231.       do 
  2232.          __is.get(__c);
  2233.       while (__is && isspace(__c));
  2234.       if (__is)
  2235.          __is.putback(__c);
  2236.     }
  2237. #else
  2238.     if (__is.flags() & ios::skipws) {
  2239.       int __c;
  2240.       do {
  2241.         __c = __buf->sbumpc();
  2242.       }
  2243.       while (__c != EOF && isspace((unsigned char)__c));
  2244.  
  2245.       if (__c == EOF) {
  2246.         __is.clear(__is.rdstate() | ios::eofbit | ios::failbit);
  2247.       }
  2248.       else {
  2249.         if (__buf->sputbackc(__c) == EOF)
  2250.           __is.clear(__is.rdstate() | ios::failbit);
  2251.       }
  2252.     }
  2253. #endif
  2254.  
  2255.     // If we arrive at end of file (or fail for some other reason) while
  2256.     // still discarding whitespace, then we don't try to read the string.
  2257.     if (__is) {
  2258.       __s.clear();
  2259.  
  2260.       size_t __n = __is.width();
  2261.       if (__n == 0)
  2262.         __n = static_cast<size_t>(-1);
  2263.       else
  2264.         __s.reserve(__n);
  2265.  
  2266.       while (__n-- > 0) {
  2267.         int __c1 = __buf->sbumpc();
  2268.         if (__c1 == EOF) {
  2269.           __is.clear(__is.rdstate() | ios::eofbit);
  2270.           break;
  2271.         }
  2272.         else {
  2273.           _CharT __c = _Traits::to_char_type(__c1);
  2274.  
  2275.           if (isspace((unsigned char) __c)) {
  2276.             if (__buf->sputbackc(__c) == EOF)
  2277.               __is.clear(__is.rdstate() | ios::failbit);
  2278.             break;
  2279.           }
  2280.           else
  2281.             __s.push_back(__c);
  2282.         }
  2283.       }
  2284.     
  2285.       // If we have read no characters, then set failbit.
  2286.       if (__s.size() == 0)
  2287.         __is.clear(__is.rdstate() | ios::failbit);
  2288.     }
  2289.  
  2290.     __is.width(0);
  2291.   }
  2292.   else                          // We have no streambuf.
  2293.     __is.clear(__is.rdstate() | ios::badbit);
  2294.  
  2295.   return __is;
  2296. }
  2297.  
  2298. template <class _CharT, class _Traits, class _Alloc>    
  2299. istream& getline(istream& __is,
  2300.                  basic_string<_CharT,_Traits,_Alloc>& __s,
  2301.                  _CharT __delim)
  2302. {
  2303.   streambuf* __buf = __is.rdbuf();
  2304.   if (__buf) {
  2305.     size_t __nread = 0;
  2306.     if (__is) {
  2307.       __s.clear();
  2308.  
  2309.       while (__nread < __s.max_size()) {
  2310.         int __c1 = __buf->sbumpc();
  2311.         if (__c1 == EOF) {
  2312.           __is.clear(__is.rdstate() | ios::eofbit);
  2313.           break;
  2314.         }
  2315.         else {
  2316.           ++__nread;
  2317.           _CharT __c = _Traits::to_char_type(__c1);
  2318.           if (!_Traits::eq(__c, __delim)) 
  2319.             __s.push_back(__c);
  2320.           else
  2321.             break;              // Character is extracted but not appended.
  2322.         }
  2323.       }
  2324.     }
  2325.  
  2326.     if (__nread == 0 || __nread >= __s.max_size())
  2327.       __is.clear(__is.rdstate() | ios::failbit);
  2328.   }
  2329.   else
  2330.     __is.clear(__is.rdstate() | ios::badbit);
  2331.  
  2332.   return __is;
  2333. }
  2334.  
  2335. template <class _CharT, class _Traits, class _Alloc>    
  2336. inline istream& 
  2337. getline(istream& __is, basic_string<_CharT,_Traits,_Alloc>& __s)
  2338. {
  2339.   return getline(__is, __s, '\n');
  2340. }
  2341.  
  2342. #endif /* __STL_USE_NEW_IOSTREAMS */
  2343.  
  2344. template <class _CharT, class _Traits, class _Alloc>
  2345. void _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,
  2346.                     _CharT* __buf,
  2347.                     size_t __n)
  2348. {
  2349.   if (__n > 0) {
  2350.     __n = min(__n - 1, __s.size());
  2351.     copy(__s.begin(), __s.begin() + __n, __buf);
  2352.     _Traits::assign(__buf[__n],
  2353.                     basic_string<_CharT,_Traits,_Alloc>::_M_null());
  2354.   }
  2355. }
  2356.  
  2357. inline const char* __get_c_string(const string& __s) { return __s.c_str(); }
  2358.  
  2359. // ------------------------------------------------------------
  2360. // Typedefs
  2361.  
  2362. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  2363. #pragma reset woff 1174
  2364. #pragma reset woff 1375
  2365. #endif
  2366.  
  2367. __STL_END_NAMESPACE
  2368.  
  2369. #include <stl_hash_fun.h>
  2370.  
  2371. __STL_BEGIN_NAMESPACE
  2372.  
  2373. template <class _CharT, class _Traits, class _Alloc>
  2374. size_t __stl_string_hash(const basic_string<_CharT,_Traits,_Alloc>& __s) {
  2375.   unsigned long __h = 0;
  2376.   for (basic_string<_CharT,_Traits,_Alloc>::const_iterator __i = __s.begin();
  2377.        __i != __s.end();
  2378.        ++__i)
  2379.     __h = 5*__h + *__i;
  2380.   return size_t(__h);
  2381. }
  2382.  
  2383. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  2384.  
  2385. template <class _CharT, class _Traits, class _Alloc>
  2386. struct hash<basic_string<_CharT,_Traits,_Alloc> > {
  2387.   size_t operator()(const basic_string<_CharT,_Traits,_Alloc>& __s) const
  2388.     { return __stl_string_hash(__s); }
  2389. };
  2390.  
  2391. #else
  2392.  
  2393. __STL_TEMPLATE_NULL struct hash<string> {
  2394.   size_t operator()(const string& __s) const
  2395.     { return __stl_string_hash(__s); }
  2396. };
  2397.  
  2398. __STL_TEMPLATE_NULL struct hash<wstring> {
  2399.   size_t operator()(const wstring& __s) const
  2400.     { return __stl_string_hash(__s); }
  2401. };
  2402.  
  2403. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  2404.  
  2405. __STL_END_NAMESPACE
  2406.  
  2407. #endif /* __SGI_STL_STRING */
  2408.  
  2409.  
  2410. // Local Variables:
  2411. // mode:C++
  2412. // End:
  2413.  
  2414.